<?php


namespace app\flash\controller;


use think\Exception;

class Kxian
{
    public function getK()
    {
        $code = input('code');
        $type = input('type');
        $res = cache($code.$type);
        if($res){
            return json(msg(1,$res,'数据返回成功!'));
        }

        if(empty($code) || empty($type)) return json(msg(0,'','请提交完整参数!'));

        $opts=array(
            "http"=>array(
                "method"=>"GET",
                "timeout"=>3,
                'header' => "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
            ),
        );
        $context = stream_context_create($opts);
        $res = file_get_contents("https://dncapi.bqiapp.com/api/coin/web-charts?code=$code&webp=1&type=$type",false, $context);
        $arr = json_decode($res,true);

        if(isset($arr['code']) && $arr['code']== 200 ){
            cache($code. $type,$arr['value'],120);
            return json(msg(1,$arr['value'],'数据返回成功!'));
        }else{
            return json(msg(0,'','暂无数据!'));
        }
    }








    public function info()
    {
        $code = input('code');
        $res = cache($code.'-info');
        if($res){
            return json(msg(1,$res,'数据返回成功!'));
        }
        $array = [
            'code'=>input('code'),
            'nonce'=> time(),
            'sign'=> md5(time()),
            'timestamp'=> date('Y-m-d H:i:s',time() - 3600 * 8),
            'token'=> '',
            'webp'=> rand(0,1)
        ];
        $data = $this->curl('https://mdncapi.bqiapp.com/api/coin/web-coininfo',true,$array,true);
        cache($code.'-info',$data,60);
        return json(msg(1,$data,'数据返回成功!'));
    }




    public function Intro()
    {
        $code = input('code');
        $res = cache($code.'-Intro');
        if($res){
            return json(msg(1,$res,'数据返回成功!'));
        }
        $opts=array(
            "http"=>array(
                "method"=>"GET",
                "timeout"=>3,
                'header' => "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
            ),
        );
        $context = stream_context_create($opts);
        $res = file_get_contents("https://www.bishijie.com/coin/$code",false, $context);
        $bds = '|<div class="left" style="width: 85%;margin-top: -4%;">[\S\s]*?<p>([\S\s]*?)</p>|';

        preg_match_all($bds,$res,$data);
        try{
            $info['Intro'] = $data[1];
        }catch (Exception $e){
            $info = [];
        }
        if(!empty($info)){
            cache($code.'-intro',$info);
            return json(msg(1,$info,'数据返回成功!'));
        }
        return json(msg(0,'','暂无数据!'));

    }

    function curl($url,$isPost = false, $postData = [], $isJson = false, array $headers = [],$cookiePath = '',$rendOrWriteCookie=1,$timeOut=3,$gzip = false)
    {
        $ch = curl_init();
        $options[CURLOPT_URL] = $url;
        $options[CURLOPT_HEADER] = false;
        $options[CURLOPT_RETURNTRANSFER] = true;
        $options[CURLOPT_SSL_VERIFYPEER] = false;
        if ($isPost) {
            $options[CURLOPT_POST] = true;
            if($isJson){
                if( is_array($postData)){
                    $postData = json_encode($postData);
                }
                array_push($headers,'Content-Type: application/json; charset=utf-8','Content-Length: ' . strlen($postData));
            }
            $options[CURLOPT_POSTFIELDS] = $postData;
        }
        if (!empty($headers)) $options[CURLOPT_HTTPHEADER] = $headers;
        if (!empty($cookiePath)){
            if ($rendOrWriteCookie == 1){
                $options[CURLOPT_COOKIEFILE] = $cookiePath;
            }else{
                $options[CURLOPT_COOKIEJAR] = $cookiePath;
            }
        }
        $options[CURLOPT_CONNECTTIMEOUT] = $timeOut;
        if($gzip)$options[CURLOPT_ENCODING] = 'gzip';
        curl_setopt_array($ch, $options);
        $data = curl_exec($ch);
        $error = curl_errno($ch);
        if($error){
            throw new Exception($error);
        }
        curl_close($ch);
        return $data;
    }
}